﻿//-----------------------------------------------------------------------
// <copyright file="ChatSystem.cs" company="FastNET Services">
//     Copyright (c) 2008-2009. All Rights Reserved.
// </copyright>
// <license>
//     GPLv2
// </license>
// <warranty>
//     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
//     BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
//     NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
//     DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// </warranty>
//-----------------------------------------------------------------------
#region Usings

using System.Collections.Generic;
using GateShift.Common.DataPackets;
using GateShift.Common.DataPackets.Chat;
using GateShift.Services.ChatSystem.Interfaces;
using GateShift.Services.SDK.Services;
using GateShift.Services.SDK.Services.Events;
using GateShift.Services.SDK.Services.HostIntegration;
#endregion

namespace GateShift.Services.ChatSystem
{
    /// <summary>
    /// General chat system service for the GateShift Server
    /// </summary>
    public class ChatSystem : ServiceBase, IChatService, ICommandLineInterfaceHandler, IClientDataHandler
    {
        #region Service Management

        /// <summary>
        /// Method to initilaize the service. 
        /// </summary>
        public override void Initialize()
        {
            ServerHost.SessionHost.SessionDisconnected += SessionHost_SessionDisconnected;
            ServerHost.ServiceHost.RegisterServiceCommandHandler("chat", this);
            base.Initialize();
        }

        #endregion

        #region ICommandLineInterfaceHandler Members

        /// <summary>
        /// Entrance method to handle command processing
        /// </summary>
        /// <param name="arguments">List of arguments to process</param>
        /// <returns>Boolean based on the success of handling a command</returns>
        public bool HandleCommand(List<string> arguments)
        {
            return false;
        }

        #endregion

        #region IClientDataHandler Members

        /// <summary>
        /// Entrance method to handle all incoming data from a client
        /// </summary>
        /// <param name="session">IClientSession that sent the packet</param>
        /// <param name="serviceCode">ServiceCode to handle</param>
        /// <param name="data">Serialized Data from the client</param>
        /// <param name="wasEncrypted">Used to continue encrypted communications</param>
        public void HandleClientData(IClientSession session, byte serviceCode, byte[] data, bool wasEncrypted)
        {
            switch ((ServerChatCode)serviceCode)
            {
                case ServerChatCode.SendMessage:
                    HandleSendMessage(session, PacketConverter.ConvertFromByteArray<ServerChatPacket>(data), wasEncrypted);
                    break;
            }
        }

        #endregion

        #region Client Data Methods

        /// <summary>
        /// Handles data for sending a message to all sessions
        /// </summary>
        /// <param name="session">Session making the request</param>
        /// <param name="packet">BasicChatPacket with information from client</param>
        /// <param name="wasEncrypted">Used to continue encrypted communications</param>
        private void HandleSendMessage(IClientSession session, ServerChatPacket packet, bool wasEncrypted)
        {
            // Send to all clients.
            ServerHost.SessionHost.SendClientData((byte)ClientServiceID.ChatService, (byte)ServerChatCode.SendMessage, PacketConverter.ConvertToByteArray(packet), wasEncrypted);
        }

        #endregion

        #region Session Events

        /// <summary>
        /// Event handler for a client disconnecting
        /// </summary>
        /// <param name="sender">Object raising this event</param>
        /// <param name="e">ClientSessionEventArgs with information about the client disconnecting</param>
        private void SessionHost_SessionDisconnected(object sender, ClientSessionEventArgs e)
        {
            ////// Get a list of all channels the client was a part of
            //// IEnumerable<KeyValuePair<Int16, ChatChannel>> channels =
            ////    (from channel in _channels where channel.Value.IsMember(e.Session.SessionID) select channel);

            ////// Go through each one and delete the channel or leave the channel
            //// foreach (KeyValuePair<Int16, ChatChannel> channel in channels)
            //// {
            ////    if (channel.Value.IsOwner(e.Session.SessionID))
            ////        DeleteChannel(channel.Key, null);
            ////    else
            ////        channel.Value.LeaveChannel(e.Session.SessionID);
            //// }
        }

        #endregion
    }
}